package com.yvon.service.demo.jdk.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author : Yvon
 * @since : 2022-01-24
 */
public class TestCyclicBarrier {

    static CyclicBarrier cyclicBarrier;

    public static void main(String[] args) {
        cyclicBarrier = new CyclicBarrier(10, () -> System.out.println("全部就绪，开始游戏"));

        for (int i = 0; i < 20; i ++ ) {
            new Thread(() -> {
                try{
                     Thread.sleep((long) (Math.random() * 3000));
                    System.out.println(Thread.currentThread().getName() + ",加载完毕！");
                    cyclicBarrier.await();
                }catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ",进入游戏！");
            }, "水友" + i + "号").start();
        }
    }
}
